require 'net/ftp'
namespace :pick do

  task :dpno => :environment do
    puts 'aaaaaaaaaaaa'
    fstr = ''
    File.open("#{Rails.root}/lib/tasks/pa30.unl", 'r') do |f|
      fstr  = f.read
    end
    flist = fstr.split("\r\n")
    flist.each do |l|
      dlist = l.split("|")
      if dlist[6].to_i == 1
        dp = Department.create(dpname: dlist[1], dpno: dlist[0], level: dlist[6].to_i + 1, master_id: 0, status: 0)
        dp.master_id = dp.id
        dp.save
      else
        dp = Department.create(dpname: dlist[1], dpno: dlist[0], level: dlist[6].to_i + 1, master_id: 0, status: 0)
        dp.master_id = Department.find_by(dpno: dlist[2]).id
        dp.save
      end
    end
  end

  task :user => :environment do
    puts "add user"
    dps = Department.where.not(dpno: '000000')
    dps.each do |dp|
      User.create(email: dp.dpno + '@boimc.com', user_name: dp.dpno, department_id: dp.id, admin: true, password: '123123123')
    end
    User.create(email: '000000@boimc.com', user_name: '000000', department_id: Department.find_by(dpno: '000000').id, admin: true, password: '123123123')

  end

  task :load_point => :environment do
    puts 'load point'
    fstr = ''
    File.open("#{Rails.root}/lib/tasks/ra30.unl", 'r') do |f|
      fstr  = f.read
    end
    flist = fstr.split("\r\n")
    flist.each do |l|
      dlist = l.split("|")
      CoreRecord.create(department_id: Department.find_by(dpno: dlist[0]).id, 
                        create_date: dlist[2],
                        stan:         dlist[3].to_i,
                        start_date:   dlist[4],
                        end_date:     dlist[5],
                        days:         dlist[6].to_i,
                        crdtype:      dlist[7].to_i,
                        crdid:        dlist[8],
                        desc:         dlist[9],
                        acid:         dlist[10].to_i,
                        acidr:        dlist[11].to_i,
                        amount:       dlist[12].to_f,
                        cust_name:    dlist[13],
                        mobile:       dlist[14],
                        account:      dlist[15],
                        open_date:    dlist[16],
                        acc_type:     dlist[17].to_i)
    end
  end
  task :clear_point => :environment do
    CoreRecord.delete_all
  end

end
